
package com.j2eefast.produce.pms.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.j2eefast.common.core.page.Query;
import com.j2eefast.common.core.utils.PageUtil;
import com.j2eefast.framework.code.service.SerialService;
import com.j2eefast.framework.sys.entity.SysUserEntity;
import com.j2eefast.framework.utils.Constant;
import com.j2eefast.framework.utils.UserUtils;
import com.j2eefast.produce.pms.entity.PmsSampleEntryEntity;
import com.j2eefast.produce.pms.entity.PmsSampleOutputEntity;
import com.j2eefast.produce.pms.entity.PmsTemplateEntity;
import com.j2eefast.produce.pms.mapper.PmsSampleEntryMapper;
import com.j2eefast.produce.pms.mapper.PmsSampleOutputMapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>样品检测数据录入</p>
 *
 * @author: summer
 * @date: 2021-05-28 16:47
 * @web: http://www.letukeji.cn
 * @version: 1.0.1
 */
@Service
@Transactional(readOnly = true)
public class PmsSampleEntryService extends ServiceImpl<PmsSampleEntryMapper, PmsSampleEntryEntity> {

	@Resource
	protected PmsSampleEntryMapper pmsSampleEntryMapper;
	@Resource
	@Lazy
	private SerialService pmsSerialService;


	/**
	 * 页面展示查询翻页
	 */
	public PageUtil findPage(Map<String, Object> params) {
		String testCode = (String) params.get("testCode");
		String testType = (String) params.get("testType");
		Page<PmsSampleEntryEntity> page = pmsSampleEntryMapper.findPage(
				new Query<PmsSampleEntryEntity>(params).getPage(),
				StrUtil.nullToDefault(testCode,""),
				StrUtil.nullToDefault(testType,""),
				(String) params.get(Constant.SQL_FILTER));
		return new PageUtil(page);
	}

	public List<PmsSampleEntryEntity> findList(Map<String, Object> params) {
		String testCode = (String) params.get("testCode");
		String testType = (String) params.get("testType");
		List<PmsSampleEntryEntity> page = pmsSampleEntryMapper.findList(
				new Query<PmsSampleEntryEntity>(params).getPage(),
				StrUtil.nullToDefault(testCode,""),
				StrUtil.nullToDefault(testType,""),
				(String) params.get(Constant.SQL_FILTER));
		return page;
	}



	@Transactional(rollbackFor = Exception.class)
	public boolean deleteByIds(Long[] ids) {

		return pmsSampleEntryMapper.deleteByIds(ids);

	}
	@Transactional(rollbackFor = Exception.class)
	public boolean updateCheck(Long id,int checkStatus) {
//		Long userId= UserUtils.getUserInfo().getId();
		String updateBy=UserUtils.getUserInfo().getName();
		return pmsSampleEntryMapper.updateCheck(id ,checkStatus,updateBy);

	}
	@Transactional(rollbackFor = Exception.class)
	public boolean issuedCheck(Long id,int checkStatus) {
//		Long userId= UserUtils.getUserInfo().getId();
		String updateBy=UserUtils.getUserInfo().getName();
		return pmsSampleEntryMapper.issuedCheck(id ,checkStatus,updateBy);

	}

	/**
	 * 添加
	 *
	 */
	@Transactional(rollbackFor = Exception.class)
	public boolean saves(PmsSampleEntryEntity taskFrom){
//		Long userId= UserUtils.getUserInfo().getId();
		Date date = new Date();//创建一个时间对象，获取到当前的时间
		taskFrom.setUpdateTime(date);
		taskFrom.setCreateTime(date);
		String userName=UserUtils.getUserInfo().getName();
		taskFrom.setCreateBy(userName);
		taskFrom.setUpdateBy(userName);
		PmsSampleEntryEntity pmsSampleEntryEntity = pmsSampleEntryMapper.selectSampleEntry();
		Long total=0L;
		if(pmsSampleEntryEntity!=null && pmsSampleEntryEntity.getTotal()!=null){
			total=pmsSampleEntryEntity.getTotal();
		}
		Map<String ,String> participant=pmsSerialService.formatTitle(total,"默认序列号");//编号
		taskFrom.setTestCode(participant.get("title"));
		taskFrom.setTotal(Long.parseLong(participant.get("total")));
		return this.save(taskFrom);

	}




}
